package com.yjd.merchant.app.api.version;
import com.yjd.comm.app.model.MerchantAppVersionModel;
import com.yjd.comm.app.service.IMerchantAppVersionService;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 商户端app版本控制
 * Created by yangchangyan on 2017/11/30.
 */
@Controller("com.yjd.api.app.AppVersionControl")
@Scope("prototype")
@RequestMapping("/merchant/app")
public class AppVersionControl extends BaseControl {

    @Resource
    private IMerchantAppVersionService merchantAppVersionService;

    /**
     * 查询最新数据包信息
     *
     * @param device_type 客户端的设备类型（ios,android）
     * @param client_app_version 客户端的版本号
     * @throws Exception
     */
    @RequestMapping(value = "/version/latest")
    public void queryLast(String device_type, Double client_app_version) throws Exception {
        if (StringUtil.isEmpty(device_type)) {
            this.errorMsg("参数[device_type]不能为空");
            return;
        }
        if (StringUtil.isEmpty(client_app_version)) {
            this.errorMsg("参数[client_app_version]不能为空");
            return;
        }

        try {
            Map<Object, Object> filter = FrameUtil.newHashMap();
            filter.put("device_type", device_type);
            filter.put("status", 0); // 0表示正常,1表示当前数据包在锁定状态无法下载使用,2数据包正在更新无法下载使用
            filter.put("sort", "last_update_time desc");
            MerchantAppVersionModel latest = merchantAppVersionService.getModelOne(filter, DbWREnums.READ);

            Map<String,Object> result = new HashMap<String,Object>();
            if(null == latest){
                result.put("status", 204);// 无新内容
                this.successData(result);
                return;
            }

            Double latest_version = latest.getApp_version();

            // 如果请求的版本小于最新版本则返回更新
            if (client_app_version.compareTo(latest_version) < 0) {
                result.put("status", 200);
                result.put("forceUpdate", latest.getForce_update());
                result.put("updateMsg", latest.getUpdate_msg());
                result.put("url", latest.getUrl());
            } else {
                result.put("status", 204);// 无新内容
            }
            result.put("vesion", NumberUtil.formatNumber(latest.getApp_version()));

            /**
             * 查询最新的强制更新版本，如果客户端的版本与最新版本之间存在强制更新版本，则将最新版本置为强制更新版本
             */
            filter.put("force_update", 1);
            MerchantAppVersionModel forceUpdateVersion = merchantAppVersionService.getModelOne(filter, DbWREnums.READ);
            if(null != forceUpdateVersion && client_app_version.compareTo(forceUpdateVersion.getApp_version()) < 0){
                result.put("forceUpdate", 1);
            }

            this.successData(result);

        } catch (Exception e) {
            this.errorMsg(e.getMessage());
        }
    }

}
